package cn.school.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

import cn.school.entity.Subject;
import cn.school.util.DBUtil;

//对应s_subject的所有操作
//返回结果的方式
public class SubjectDao {
	// CRUD
	public int update(Subject s) {
		String sql = "update s_subject set subjectname=?,classhour=?,gradeid=? where id=?";
		return 0;
	}

	public int del(long id) {
		return 0;
	}

	public int add(Subject s) {
		String sql = "insert into s_subject";
		return 0;
	}

	// >>>R
	// 1:查询全部
	public List<Subject> search() {
		List<Subject> datas = new java.util.ArrayList<>();
		try (Connection con = DBUtil.getConnection();
				PreparedStatement ps = con.prepareStatement("select * from s_subject");
				ResultSet rs = ps.executeQuery();) {
			while (rs.next()) {
				datas.add(new Subject(rs.getLong("id"), rs.getString("subjectname"), rs.getInt("classhour"),
						rs.getLong("gradeid")));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return datas;
	}

	// 分页查询
	public List<Subject> search(int pageNo, int pageSize) {
		int offset = (pageNo - 1) * pageSize;

		List<Subject> datas = new java.util.ArrayList<>();
		try {
			Connection con = DBUtil.getConnection();
			PreparedStatement ps = con.prepareStatement("select * from s_subject");
			ResultSet rs = ps.executeQuery();

			while (rs.next()) {
				datas.add(new Subject(rs.getLong("id"), rs.getString("subjectname"), rs.getInt("classhour"),
						rs.getLong("gradeid")));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} // close
		return datas;
	}

	// 查询单个:找不到是null,找到了,返回一个对象
	public Subject get(long id) {

		return null;
	}
}
